package com.jdwx.opengl.face.dface.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SwitchCompat;

import com.jdwx.opengl.R;
import com.jdwx.opengl.face.dface.DFaceApplication;
import com.jdwx.opengl.face.utils.FaceConfig;
import com.jdwx.opengl.face.utils.JSONUtil;
import com.jdwx.opengl.face.utils.StringUtil;
import com.jdwx.opengl.face.utils.ToastUtil;
import com.jdwx.opengl.face.utils.Tools;


/**
 * 人脸识别设置
 */
public class FaceConfigActivity extends AppCompatActivity implements View.OnClickListener {
    Button btn_reset;

    Spinner spinner_color_camera;
    Spinner spinner_ir_camera;
    Spinner spinner_camera_size;
    Spinner spinner_camera_rotate;
    SwitchCompat switch_camera_switch;
    Spinner spinner_min_detect;
    Spinner spinner_detect_precision;
    Spinner spinner_recognize_size;
    Spinner spinner_confidence;
    Spinner spinner_pat_confidence;
    Spinner spinner_live_level;
    SwitchCompat switch_rgb_anti;
    SwitchCompat switch_quality_filter;
    SwitchCompat switch_exposure_filter;
    String strCameraSize = "";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_chevron_left_primary);
        getSupportActionBar().setTitle( "人脸识别参数配置" );
        setContentView(R.layout.activity_face_config);

        btn_reset = findViewById(R.id.btn_reset);
        spinner_color_camera = findViewById(R.id.spinner_color_camera);
        spinner_ir_camera = findViewById(R.id.spinner_ir_camera);
        spinner_camera_size= findViewById(R.id.spinner_camera_size);
        spinner_camera_rotate = findViewById(R.id.spinner_camera_rotate);
        switch_camera_switch = findViewById(R.id.switch_camera_switch);
        spinner_min_detect = findViewById(R.id.spinner_min_detect);
        spinner_detect_precision = findViewById(R.id.spinner_detect_precision);
        spinner_recognize_size = findViewById(R.id.spinner_recognize_size);
        spinner_confidence = findViewById(R.id.spinner_confidence);
        spinner_pat_confidence = findViewById(R.id.spinner_pat_confidence);
        spinner_live_level = findViewById(R.id.spinner_live_level );
        switch_rgb_anti = findViewById(R.id.switch_rgb_anti );
        switch_quality_filter = findViewById(R.id.switch_quality_filter);
        switch_exposure_filter = findViewById(R.id.switch_exposure_filter);
        btn_reset.setOnClickListener( this );

        runOnUiThread(() -> initCtls());
        runOnUiThread(() -> setDefault());
    }

    /**
     * 初始化数据
     */
    void initCtls(){
        spinner_color_camera.setAdapter(ArrayAdapter.createFromResource(this, R.array.face_color_camera, android.R.layout.simple_spinner_item));
        spinner_ir_camera.setAdapter(ArrayAdapter.createFromResource(this, R.array.face_ir_camera, android.R.layout.simple_spinner_item));
        spinner_camera_size.setAdapter(ArrayAdapter.createFromResource(this, R.array.face_camera_size, android.R.layout.simple_spinner_item));
        spinner_camera_rotate.setAdapter(ArrayAdapter.createFromResource(this, R.array.face_camera_rotate, android.R.layout.simple_spinner_item));

        spinner_min_detect.setAdapter(ArrayAdapter.createFromResource(this, R.array.face_min_detect, android.R.layout.simple_spinner_item));
        spinner_detect_precision.setAdapter(ArrayAdapter.createFromResource(this, R.array.face_detect_precision, android.R.layout.simple_spinner_item));
        spinner_recognize_size.setAdapter(ArrayAdapter.createFromResource(this, R.array.face_recognize_size, android.R.layout.simple_spinner_item));
        spinner_confidence.setAdapter(ArrayAdapter.createFromResource(this, R.array.face_confidence, android.R.layout.simple_spinner_item));
        spinner_pat_confidence.setAdapter(ArrayAdapter.createFromResource(this, R.array.face_pat_confidence, android.R.layout.simple_spinner_item));
        spinner_live_level.setAdapter(ArrayAdapter.createFromResource(this, R.array.face_live_level, android.R.layout.simple_spinner_item));
    }
    /**
     * 设置默认值
     */
    void setDefault(){
        spinner_color_camera.setSelection(Tools.findArrayIndex(getResources().getStringArray(R.array.face_color_camera), DFaceApplication.faceConfig.COLOR_CAMERAID+""));
        spinner_ir_camera.setSelection(Tools.findArrayIndex(getResources().getStringArray(R.array.face_ir_camera), DFaceApplication.faceConfig.IR_CAMERAID+""));
        strCameraSize= DFaceApplication.faceConfig.CAMERA_WIDTH+"x"+ DFaceApplication.faceConfig.CAMERA_HEIGHT;
        spinner_camera_size.setSelection(Tools.findArrayIndex(getResources().getStringArray(R.array.face_camera_size), strCameraSize));
        spinner_camera_rotate.setSelection(Tools.findArrayIndex(getResources().getStringArray(R.array.face_camera_rotate), DFaceApplication.faceConfig.CAMERA_ADDITIONAL_ROTATE+""));
        switch_camera_switch.setChecked(DFaceApplication.faceConfig.CAMERA_IS_MIRROW);

        spinner_min_detect.setSelection(Tools.findArrayIndex(getResources().getStringArray(R.array.face_min_detect), DFaceApplication.faceConfig.DETECT_MIN_SIZE+""));
        spinner_detect_precision.setSelection(DFaceApplication.faceConfig.DETECT_PRECISION);
        spinner_recognize_size.setSelection(Tools.findArrayIndex(getResources().getStringArray(R.array.face_recognize_size), DFaceApplication.faceConfig.RECOGNIZE_MIN_SIZE+""));
        spinner_confidence.setSelection(Tools.findArrayIndex(getResources().getStringArray(R.array.face_confidence), DFaceApplication.faceConfig.CONFIDENCE+""));
        spinner_pat_confidence.setSelection(Tools.findArrayIndex(getResources().getStringArray(R.array.face_pat_confidence), DFaceApplication.faceConfig.RGB_LIVENESS_THRES+""));
        spinner_live_level.setSelection(Tools.findArrayIndex(getResources().getStringArray(R.array.face_live_level), DFaceApplication.faceConfig.RGB_LIVENESS_LEVEL+""));
        switch_rgb_anti.setChecked(DFaceApplication.faceConfig.ENABLE_RGBLIVENESS_FILTER);
        switch_quality_filter.setChecked(DFaceApplication.faceConfig.ENABLE_QUALITY_FILTER);
        switch_exposure_filter.setChecked(DFaceApplication.faceConfig.ENABLE_EXPOSURE_FILTER);


    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.menu.face_top_actions, menu);
        return super.onCreateOptionsMenu(menu);
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int itemId_ = item.getItemId();
        if (itemId_ == android.R.id.home) {
            finish();
            return true;
        }
        if (itemId_ == R.id.save) {
            submit();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * 保存设置的数据
     */
    void submit(){
        DFaceApplication.faceConfig.COLOR_CAMERAID = StringUtil.stringToInt(getResources().getStringArray(R.array.face_color_camera)[(int)spinner_color_camera.getSelectedItemId()],0);
        DFaceApplication.faceConfig.IR_CAMERAID = StringUtil.stringToInt(getResources().getStringArray(R.array.face_ir_camera)[(int)spinner_ir_camera.getSelectedItemId()],0);
        strCameraSize = getResources().getStringArray(R.array.face_camera_size)[(int)spinner_camera_size.getSelectedItemId()];
        String[] arrCameraSize = strCameraSize.split("x");
        DFaceApplication.faceConfig.CAMERA_HEIGHT = StringUtil.stringToInt(arrCameraSize[1],0);
        DFaceApplication.faceConfig.CAMERA_WIDTH = StringUtil.stringToInt(arrCameraSize[0],0);
        DFaceApplication.faceConfig.CAMERA_ADDITIONAL_ROTATE = StringUtil.stringToInt(getResources().getStringArray(R.array.face_camera_rotate)[(int)spinner_camera_rotate.getSelectedItemId()],0);
        DFaceApplication.faceConfig.CAMERA_IS_MIRROW = switch_camera_switch.isChecked();

        DFaceApplication.faceConfig.DETECT_MIN_SIZE = StringUtil.stringToInt(getResources().getStringArray(R.array.face_min_detect)[(int)spinner_min_detect.getSelectedItemId()],0);
        DFaceApplication.faceConfig.DETECT_PRECISION = (int)spinner_detect_precision.getSelectedItemId();
        DFaceApplication.faceConfig.RECOGNIZE_MIN_SIZE = StringUtil.stringToInt(getResources().getStringArray(R.array.face_recognize_size)[(int)spinner_recognize_size.getSelectedItemId()],0);
        DFaceApplication.faceConfig.CONFIDENCE = StringUtil.stringToFloat(getResources().getStringArray(R.array.face_confidence)[(int)spinner_confidence.getSelectedItemId()]);
        DFaceApplication.faceConfig.RGB_LIVENESS_THRES = StringUtil.stringToFloat(getResources().getStringArray(R.array.face_pat_confidence)[(int)spinner_pat_confidence.getSelectedItemId()]);
        DFaceApplication.faceConfig.RGB_LIVENESS_LEVEL = StringUtil.stringToInt(getResources().getStringArray(R.array.face_live_level)[(int)spinner_live_level.getSelectedItemId()],0);
        DFaceApplication.faceConfig.ENABLE_RGBLIVENESS_FILTER = switch_rgb_anti.isChecked();
        DFaceApplication.faceConfig.ENABLE_QUALITY_FILTER = switch_quality_filter.isChecked();
        DFaceApplication.faceConfig.ENABLE_EXPOSURE_FILTER = switch_exposure_filter.isChecked();

        DFaceApplication.setFaceConfig();
        Log.e("FaceSettingActivity", JSONUtil.Serializable(DFaceApplication.faceConfig));
        ToastUtil.showToast(this,"配置保存成功");

        this.finish();
    }

    @Override
    public void onClick(View v) {
        //重置默认
        if (v.getId() == R.id.btn_reset) {
            DFaceApplication.faceConfig= new FaceConfig();
            DFaceApplication.setFaceConfig();
            Log.e("FaceConfigActivity", JSONUtil.Serializable(DFaceApplication.faceConfig));
            ToastUtil.showToast(this,"操作完成");
            runOnUiThread(() -> setDefault());
        }
    }
}